﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using Com.CMS263.Util;
using Com.CMS263.BLL;

namespace Com.CMS263.Web.manage.ManageImage
{
    public partial class image_add : System.Web.UI.Page
    {
        int mid = 0;
        string type = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            mid = Convert.ToInt32(Request["mid"]);
            type = Request["type"];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ImageBLL ibll = new ImageBLL();
            Com.CMS263.PO.Image entity = new Com.CMS263.PO.Image();
           
            string name = Request["txtName"];
            
            int order = Convert.ToInt32(Request["txtOrder"]);
            
            string val = "";
            //文件按天存储，没有目录就自己创建
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            string savePath = Server.MapPath("/ArtUpFile/" + strDay);
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            HttpPostedFile hpf = Request.Files["fileCover"];
            //string ext = hpf.FileName.Substring(hpf.FileName.LastIndexOf('.'));
            //string okext = "jpg,gif,jpeg,png";
            //if (okext.IndexOf(ext.ToLower()) < 0)
            //{
            //    StringUtil.Alert("图片类型请上传" + okext, "-1");
            //}
            if (hpf.ContentLength < 5)
            {
                StringUtil.Alert("图片是必填的", "-1");
            }
            else
            {
                val = strDay + "/" + UpLoadFileUtil.UpLoadFile(Request.Files["fileCover"], savePath);
            }

            entity.Name = name;
            entity.OrderNum = order;
            entity.Path = val;
            entity.AddDate = DateTime.Now;
            entity.ModelId = mid;
            entity.Type = type;
            ibll.entity = entity;
            if (ibll.Add())
            {
                StringUtil.Alert("添加成功", "imagelist.aspx?mid="+mid+"&type="+type);
            }
            else
            {
                StringUtil.Alert("添加失败", "-1");
            }
        }
    }
}
